package com.ndp.fb.rdb.db.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.DictionaryRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.PstAssign;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.AdAccountPermission;
import com.ndp.fb.rdb.model.Dictionary;
import com.ndp.fb.util.StringUtil;

/**
 * Created by evan.wan on 2015/6/23.
 */
@Service
public class DictionaryRdbServiceImpl extends DaoTemplate implements DictionaryRdbService {

    @Override
    public List<Dictionary> getDict(String type)
    {
        String sql1 = "SELECT * FROM t_dictionary WHERE dict_type = ? ORDER BY id";
        String sql2 = "SELECT * FROM t_dictionary ORDER BY id ";
        if(StringUtil.isEmpty(type))
            return getJdbcTemplate().query(sql2, DefaultRowMapper.autoRowMapper(Dictionary.class));
        else
            return getJdbcTemplate().query(sql1, new Object[]{type}, DefaultRowMapper.autoRowMapper(Dictionary.class));
    }

    @Override
    public List<Dictionary> getDictEN(String type)
    {
        String sql1 = "SELECT d.id,d.dict_type,d.en_dict_name AS dict_name,d.dict_value FROM t_dictionary d WHERE d.dict_type = ? ORDER BY id";
        String sql2 = "SELECT d.id,d.dict_type,d.en_dict_name AS dict_name,d.dict_value FROM t_dictionary d ORDER BY id ";
        if(StringUtil.isEmpty(type))
            return getJdbcTemplate().query(sql2, DefaultRowMapper.autoRowMapper(Dictionary.class));
        else
            return getJdbcTemplate().query(sql1, new Object[]{type}, DefaultRowMapper.autoRowMapper(Dictionary.class));
    }


    @Override
    public Dictionary getDictionaryByCode(String languageCode) {

        String sql = "select * from t_dictionary where dict_value = ? and dict_type = 'version'";
        return findOne(sql, new Object[] {languageCode}, DefaultRowMapper.autoRowMapper(Dictionary.class));

    }
}
